﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.Hotel.UI.Controls;

namespace WHC.Hotel.UI
{
    public partial class FrmTeaSuiteView : BaseForm
    {
        public FrmTeaSuiteView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新所有房间的状态显示
        /// </summary>
        public void UpdateStatus()
        {
            foreach (TabPage page in this.tabControl1.TabPages)
            {
                foreach (Control control in page.Controls)
                {
                    TeaSuiteViewControl lvwControl = control as TeaSuiteViewControl;
                    if (lvwControl != null)
                    {
                        lvwControl.UpdateStatus();
                    }
                }
                page.Refresh();
            }
        }

        public void ChangeViewType(View viewType)
        {
            foreach (TabPage page in this.tabControl1.TabPages)
            {
                foreach (Control control in page.Controls)
                {
                    TeaSuiteViewControl lvwControl = control as TeaSuiteViewControl;
                    if (lvwControl != null)
                    {
                        lvwControl.ChangeViewType(viewType);
                    }
                }
                page.Refresh();
            }
        }

        public void ChangeRoomStatus(string roomStatus)
        {
            foreach (TabPage page in this.tabControl1.TabPages)
            {
                foreach (Control control in page.Controls)
                {
                    TeaSuiteViewControl lvwControl = control as TeaSuiteViewControl;
                    if (lvwControl != null)
                    {
                        lvwControl.ChangeRoomStatus(roomStatus);
                    }
                }
                page.Refresh();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region 根据不同的房间类型创建不同的Tab和房间视图
            this.tabControl1.TabPages.Clear();
            List<TeaSuiteTypeInfo> roomTypeList = BLLFactory<TeaSuiteType>.Instance.GetAll();
            foreach (TeaSuiteTypeInfo info in roomTypeList)
            {
                TabPage page = new TabPage();
                page.Text = info.Name;
                page.Tag = info;

                TeaSuiteViewControl viewControl = new TeaSuiteViewControl();
                viewControl.RoomType = info.Name;
                viewControl.Dock = DockStyle.Fill;
                viewControl.OnShowStatus = new TeaSuiteViewControl.ShowStatusHandler(OnShowStatus);

                page.Controls.Clear();
                page.Controls.Add(viewControl);
                this.tabControl1.TabPages.Add(page);
            } 
            #endregion
        }

        private List<CListItem> GetRoomDefaultItems(TeaSuiteInfo roomInfo)
        {
            List<CListItem> subItems = new List<CListItem>();
            string price = "0";
            TeaSuiteTypeInfo typeInfo = BLLFactory<TeaSuiteType>.Instance.FindByName(roomInfo.RoomType);

            subItems.Add(new CListItem("最低消费：", typeInfo.AtLeaseMoney.ToString("f2")));
            subItems.Add(new CListItem("预设单价：", typeInfo.Price.ToString("f2")));
            subItems.Add(new CListItem("房间状态：", roomInfo.Status.ToString()));
            subItems.Add(new CListItem("服务生：", roomInfo.Waiter));
            subItems.Add(new CListItem("所在区域：", roomInfo.Region));
            subItems.Add(new CListItem("进店时间：", ""));
            subItems.Add(new CListItem("已用时间：", ""));
            subItems.Add(new CListItem("已交金额：", ""));
            subItems.Add(new CListItem("应收金额：", ""));

            return subItems;
        }

        public void InitDisplayItems(Dictionary<string, List<CListItem>> DisplayItems,
            TeaSuiteInfo roomInfo, decimal allMoney)
        {
            #region 构造测试内容

            DisplayItems.Clear();
            List<CListItem> subItems = new List<CListItem>();

            if (roomInfo.Status == TeaSuiteStatus.占用)
            {
                TeaOrderInfo orderInfo = BLLFactory<TeaOrder>.Instance.GetCurrentOrder(roomInfo.RoomNo);
                if (orderInfo != null)
                {
                    subItems.Add(new CListItem("最低消费：", orderInfo.AtLeaseMoney.ToString("f2")));
                    subItems.Add(new CListItem("预设单价：", orderInfo.Price.ToString("C2")));
                    subItems.Add(new CListItem("房间状态：", roomInfo.Status.ToString()));
                    subItems.Add(new CListItem("所在区域：", roomInfo.Region));
                    subItems.Add(new CListItem("进店时间：", orderInfo.CreateDate.ToString()));

                    TimeSpan ts = DateTime.Now.Subtract(orderInfo.CreateDate);
                    int hour = ts.Hours + ts.Days * 24;
                    string timeString = string.Format("{0}小时{1}分", hour, ts.Minutes);
                    subItems.Add(new CListItem("已用时间：", timeString));
                    subItems.Add(new CListItem("已交押金：", orderInfo.DepositMoney.ToString("C2")));

                    //复杂计算出来
                    subItems.Add(new CListItem("应收金额：", allMoney.ToString("C2")));
                }
                else
                {
                    subItems = GetRoomDefaultItems(roomInfo);
                }
            }
            else
            {
                subItems = GetRoomDefaultItems(roomInfo);
            }
            DisplayItems.Add(roomInfo.RoomType + roomInfo.RoomNo, subItems);

            TeaSuiteStatusInfo statusInfo = BLLFactory<TeaSuite>.Instance.GetStatus();
            subItems = new List<CListItem>();
            subItems.Add(new CListItem("房间总数：", statusInfo.All.ToString()));
            subItems.Add(new CListItem("当前可供：", statusInfo.Availables.ToString()));
            subItems.Add(new CListItem("当前占用：", statusInfo.Occupies.ToString()));
            subItems.Add(new CListItem("当前预定：", statusInfo.BookIns.ToString()));
            subItems.Add(new CListItem("当前停用：", statusInfo.Stops.ToString()));
            subItems.Add(new CListItem("总入住率：", statusInfo.InRoomRate));
            DisplayItems.Add("总房态", subItems);
            #endregion
        }

        private void OnShowStatus(TeaSuiteInfo roomInfo)
        {
            decimal allMoney = 0.0M;

            #region 更新消费记录
            if (roomInfo != null)
            {
                List<TeaConsumerListInfo> consumerList = BLLFactory<TeaSuite>.Instance.GetAllConsumption(roomInfo.RoomNo);

                this.listView1.Items.Clear();
                int i = 1;
                foreach (TeaConsumerListInfo info in consumerList)
                {
                    ListViewItem item = new ListViewItem(i.ToString());
                    item.SubItems.Add(info.RoomNo);

                    string itemName = info.ItemName;
                    if (info.ItemName.Contains("房间"))
                    {
                        if (info.ItemName.Contains("房间买断消费"))
                        {
                            itemName = info.ItemName + "(买断计费)";
                        }
                        else
                        {
                            itemName = info.ItemName + "(计时计费)";
                        }
                    }
                    item.SubItems.Add(itemName);
                    //item.SubItems.Add(info.ItemName + (info.ItemName.Contains("房间买断消费") ? " (买断计费)" : " (计时计费)"));
                    
                    item.SubItems.Add(info.Price.ToString("C2"));
                    item.SubItems.Add(info.Discount.ToString());
                    item.SubItems.Add(info.DiscountPrice.ToString("C2"));
                    item.SubItems.Add(info.Quantity.ToString());
                    item.SubItems.Add(info.Amount.ToString("C2"));
                    item.SubItems.Add(info.BeginTime.ToString());
                    item.SubItems.Add(info.Waiter);
                    item.SubItems.Add(info.Creator);

                    if (info.Quantity < 0)
                    {
                        item.ForeColor = Color.Red;
                    }

                    this.listView1.Items.Add(item);
                    allMoney += info.Amount;
                    i++;
                }
            } 
            #endregion

            #region 更新房间信息
            FrmStatus dlg = Portal.gc.MainDialog.mainStatus;
            if (dlg != null)
            {
                if (roomInfo != null)
                {
                    InitDisplayItems(dlg.DisplayItems, roomInfo, allMoney);
                    dlg.UpdateContent();
                }
                else
                {
                    dlg.InitDisplayItems();
                    dlg.UpdateContent();
                }
            }
            //Portal.gc.MainDialog.ShowMainStatusWin(); 

            #endregion

            this.lblAmount.Text = string.Format("消费总金额：{0:C2}", allMoney);
        }
        
        private void menu_LargIcon_Click(object sender, EventArgs e)
        {
            ChangeViewType(View.LargeIcon);
            ClearViewType(sender);
        }

        private void menu_SmallIcon_Click(object sender, EventArgs e)
        {
            ChangeViewType(View.SmallIcon);
            ClearViewType(sender);
        }

        private void menu_Detail_Click(object sender, EventArgs e)
        {
            ChangeViewType(View.Details);
            ClearViewType(sender);
        }

        private void ClearViewType(object sender)
        {
            foreach (ToolStripItem item in this.tsbViewType.DropDown.Items)
            {
                ToolStripMenuItem menuItem = item as ToolStripMenuItem;
                if (menuItem != null)
                {
                    menuItem.Checked = false;
                }
            }

            ToolStripMenuItem selectItem = sender as ToolStripMenuItem;
            selectItem.Checked = true;
            this.tsbViewType.Text = selectItem.Text;
        }

        private void ClearStatus(object sender)
        {
            foreach (ToolStripItem item in this.tsbStatus.DropDown.Items)
            {
                ToolStripMenuItem menuItem = item as ToolStripMenuItem;
                if(menuItem != null)
                {
                    menuItem.Checked = false;
                }
            }
            ToolStripMenuItem selectItem = sender as ToolStripMenuItem;
            selectItem.Checked = true;
            this.tsbStatus.Text = selectItem.Text;
        }

        private void menu_Status_Avalible_Click(object sender, EventArgs e)
        {
            ClearStatus(sender);
            ChangeRoomStatus(TeaSuiteStatus.可供.ToString());
        }

        private void menu_Status_Use_Click(object sender, EventArgs e)
        {
            ClearStatus(sender);
            ChangeRoomStatus(TeaSuiteStatus.占用.ToString());
        }

        private void menu_Status_Stop_Click(object sender, EventArgs e)
        {
            ClearStatus(sender);
            ChangeRoomStatus(TeaSuiteStatus.停用.ToString());
        }

        private void menu_Status_Book_Click(object sender, EventArgs e)
        {
            ClearStatus(sender);
            ChangeRoomStatus(TeaSuiteStatus.预定.ToString());
        }

        private void menu_Status_All_Click(object sender, EventArgs e)
        {
            ClearStatus(sender);
            ChangeRoomStatus("");
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            UpdateStatus();
        }
    }
}
